//
//  Content.m
//  Finder
//
//  Created by cdm on 11-3-18.
//  Copyright 2011年 __MyCompanyName__. All rights reserved.
//

#import "Content.h"


@implementation Content

@synthesize name;
@synthesize type;
@synthesize data;
@synthesize icon;
@synthesize children;

+ (Content *)contentFromDict:(NSDictionary *)dict
{
    Content *content = [[[Content alloc] init] autorelease];
    
    NSString *name = [dict objectForKey:@"Name"];
    int type = [[dict objectForKey:@"Type"] intValue];
    NSString * data = [dict objectForKey:@"Data"];
    NSString * icon = [dict objectForKey:@"Icon"];
    
    content.name = name;
    content.type = type;
    content.data = data;
    content.icon = icon;
    
    NSArray *childrenDict = [dict objectForKey:@"Children"];
    for (NSDictionary *childDict in childrenDict) {
        Content * childContent = [self contentFromDict:(childDict)];
        [content.children addObject:childContent];
    }
    
    return content;
}

+ (NSMutableArray *)contentsFromPlist:(NSString *)plistName
{
    NSMutableArray *contents = [[[NSMutableArray alloc] init] autorelease];
    
    NSString *path = [[NSBundle mainBundle] pathForResource:plistName ofType:nil inDirectory:nil forLocalization:[AppLanguage getLanguageAsString]];

    NSArray *dicts = [[NSArray alloc] initWithContentsOfFile:path];
    
    for (NSDictionary *dict in dicts) {
        Content *childContent = [self contentFromDict:dict];
        [contents addObject:childContent];
    }
    
    [dicts release];

    return contents;
}

- (id)init
{
    self = [super init];
    if (self) {
        self.children = [[NSMutableArray alloc] init];
    }
    
    return self;
}

- (void)dealloc
{
    NSLog(@"Content %@ dealloc", self.name);
    
    [self.name release];
    [self.data release];
    [self.icon release];
    [self.children release];
    
    [super dealloc];
}

@end
